//
//  ViewController.swift
//  launchImageCheck
//
//  Created by xia on 15/7/8./Users/macbookpro/Desktop/ios启动界面
//  Copyright (c) 2015年 xiangha. All rights reserved.
//

import Cocoa
class ViewController: NSViewController {
    //期望的数组
    let expectSizeArray=["320*480","640*960","640*1136","750*1334","768*1024","768*1004","1242*2208","1536*2048","1536*2008"];
    var textField:NSTextField!
    var missSizeStr:String!
    var missCnt=0
    override func viewDidLoad() {
        super.viewDidLoad()
        var windows=NSApplication.sharedApplication().windows

       var closeBtn=windows[0].standardWindowButton(NSWindowButton.CloseButton)!

        closeBtn.target=self
        closeBtn.action="closeClick:"
        self.view.frame=NSRect(x: 0, y: 0, width: 800, height: 500)
        var needSizeTextField=NSTextField()
        self.view.addSubview(needSizeTextField);
        needSizeTextField.textColor=NSColor.redColor();
        needSizeTextField.backgroundColor=NSColor.clearColor()
        needSizeTextField.font=NSFont(name: "Palatino", size: 20)
        var needSizeStr=""
        needSizeTextField.enabled=false
        for i in 0...expectSizeArray.count-1{
            if i<expectSizeArray.count-1 {
                needSizeStr+=expectSizeArray[i]+"、"
            }
            else{
                needSizeStr+=expectSizeArray[i]

            }
        }
        needSizeTextField.stringValue="所需尺寸："+"\(expectSizeArray.count)"+"个"+"\n"+needSizeStr


        var btn=NSButton()
        btn.title="打开文件"
        self.view.addSubview(btn)
        btn.bezelStyle=NSBezelStyle.SmallSquareBezelStyle
        btn.mas_makeConstraints { (make) -> Void in
            make.top.equalTo()(50)

            make.right.equalTo()(-20)

            make.width.equalTo()(150)
            make.height.equalTo()(150)
        }
        btn.target=self
        btn.action="btnClick:"





        textField=NSTextField();
        self.view.addSubview(textField);

        textField.textColor=NSColor.redColor();
        textField.font=NSFont(name: "Palatino", size: 20)
        textField.stringValue=""
        textField.enabled=false
        textField.mas_makeConstraints { (make) -> Void in
            make.top.equalTo()(btn.mas_bottom).offset()(20)
            make.left.equalTo()(20)
            make.right.equalTo()(-20)
            make.height.equalTo()(250)
        }



        needSizeTextField.mas_makeConstraints { (make) -> Void in
            make.top.equalTo()(btn)
            make.left.equalTo()(20)
            make.right.equalTo()(btn.mas_left).offset()(-20)

            make.height.equalTo()(btn.mas_height)
        }

    }
    func btnClick(sender:AnyObject){
        println("btn click")

        var openPanel=NSOpenPanel()
        openPanel.title="启动图尺寸检查"
//        openPanel.minSize=NSSize(width: 1800, height: 1800);
        openPanel.canChooseDirectories=true
        var i=openPanel.runModal()
        if i==NSModalResponseOK {
            var filepath:String!=openPanel.URL?.relativePath

            var fm=NSFileManager.defaultManager()
            var fileArray=fm.subpathsAtPath(filepath)

            checkMissSizeWithFileArray(fileArray!,filePath: filepath)
            if missSizeStr.isEmpty  {
                textField.stringValue="OK !";

            }
            else{
                missSizeStr.removeAtIndex(advance(missSizeStr.endIndex, -1))
                textField.stringValue="缺少尺寸："+"\(missCnt)"+"个"+"\n"+missSizeStr;
            }


            for index in 0...fileArray!.count-1 {
                let name:String=fileArray![index] as! String
                if name.hasSuffix(".png"){
                    var path=filepath+"/"+name
                    var image=NSImage(contentsOfFile: path)
                    var imageSize=image!.size
                    var isRight=checkSizeIsRight(imageSize)
                    var width=Int(imageSize.width)
                    var height=Int(imageSize.height)
                    if isRight {

                        var newFilePath=filepath.stringByAppendingPathComponent("welcome_\(width)_\(height).png")
                        fm.moveItemAtPath(path, toPath: newFilePath, error: nil)
                    }
                    else{
                        fm.removeItemAtPath(path, error: nil)
                    }

                }
            }




        }

    }
    func checkSizeIsRight(size:NSSize?)->Bool{


        for i in 0...expectSizeArray.count-1 {
            var sizeStr=expectSizeArray[i]

            var width=sizeStr.componentsSeparatedByString("*")[0]
            var height=sizeStr.componentsSeparatedByString("*")[1]

            var expectSize=NSSize(width: width.toInt()!, height: height.toInt()!)
            if size==expectSize {
                 return true
            }

        }
        return false
    }
    func checkMissSizeWithFileArray(fileArray:NSArray,filePath:String){
        missSizeStr=""
        missCnt=0
        for i in 0...expectSizeArray.count-1 {
            var sizeStr=expectSizeArray[i]

            var expectW=sizeStr.componentsSeparatedByString("*")[0].toInt()!
            var expectH=sizeStr.componentsSeparatedByString("*")[1].toInt()!

            var flag=0
            for index in 0...fileArray.count-1 {
                let name:String=fileArray[index] as! String
                if name.hasSuffix(".png"){
                    var path=filePath+"/"+name
                    var image=NSImage(contentsOfFile: path)
                    var imageSize=image!.size
                    var isRight=checkSizeIsRight(imageSize)
                    var width=Int(imageSize.width)
                    var height=Int(imageSize.height)
                    if expectW==width&&expectH==height {
                        flag=1
                        break;
                    }

                }
            }
            if flag==0{
                missSizeStr=missSizeStr+"\(expectW)*\(expectH)"+"、"
                println("\(expectW)*\(expectH)")
                missCnt++

            }

        }
    }
    func closeClick(sender:AnyObject){
        println("close")
        exit(0)

    }
     func windowWillClose(notification: NSNotification) {
        exit(0)
    }
    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }


}

